<template>
    <importDialog
      :dialogInfo="importDialogInfo"
      @baseMounted="baseMounted"
      :self="self">
    </importDialog>
</template>


<script>
import { ref, unref, watch, onMounted } from 'vue'
import { post } from "/src/api/index";
import importDialog from './edit_dialog.vue'
import { ElMessage } from '/src/utils'
import dayjs from 'dayjs'
import { funcSortColumns } from '/src/utils'

export default {
    emits: ['baseMounted'],
    components:{
      importDialog,
    },
    props: {
      self:{},
      dialogInfo: {},
    },
    setup(prop,context) {

        let columns = []
        const importDialogInfo = ref({
          title: prop.dialogInfo.title || '未知',
          visible:false,
          loading: false,
          rules:[],
          form:{},
          columns:[
            ...columns,
            {
              prop: 'file',
              label: '文件',
              discard: true,
              hide: true,
            },
            {
              prop: 'tip_error_msg',
              label: '失败信息',
              discard: true,
              hide: true,
            },
            {
              prop: 'tip_error_data',
              label: '失败信息',
              discard: true,
              hide: true,
            },
            {
              prop: 'tip_total_count',
              label: '总条目',
              discard: true,
              hide: true,
              columnStyle: 'width:60vw',
            },
            {
              prop: 'tip_done_count',
              label: '已完成条目',
              discard: true,
              hide: true,
              columnStyle: 'width:60vw',
            },
            {
              prop: 'file_list',
              label: '上传文件',
              type: 'upload',
              discard: true,
              columnStyle: 'width:60vw',
              onselectfile: (file, fileList) => {
                let dialogInfo = unref(importDialogInfo)
                dialogInfo.form.file = file.raw
                if(dialogInfo.onselectfile) {
                  dialogInfo.onselectfile(file)
                }
              }
            },
            {
              prop: 'tip',
              label: '提示信息',
              type: 'textarea',
              discard: true,
              rows: 8,
              columnStyle: 'width:60vw',
            },
          ],
          keys: {
            ...(prop.dialogInfo.keys||[])
          },
          values: {
            ...(prop.dialogInfo.values||[])
          },
          excel_date_format_map: {
            ...(prop.dialogInfo.excel_date_format_map||{mdate:'yyyy-MM-dd'})
          },
          onconfirm: (param) => {

            ElMessage.error('not implement');
            dialogInfo.loading = false
          },
          show: (param) => {
            const {dialogInfo, query} = param

            dialogInfo.columns.forEach( item => {
              if (item.type == 'upload') {
                try {
                  let childNodes = document.getElementsByClassName('el-upload-list')[0].childNodes
                  let childNodesCount = childNodes.length
                  for(let childIndex = childNodesCount - 1; childIndex >= 0; childIndex--) {
                    childNodes[childIndex].remove()
                  }
                }catch(ex) {

                }

              }
              if (item.oninit) {
                item.oninit({...self, mode: 'init', dialogInfo:dialogInfo, column:item, columns: dialogInfo.columns, form: dialogInfo.form})
              }
            })

            dialogInfo.form.workspace_name = query.workspace_name
            if (query.workspace_name) {
              let column = dialogInfo.columns.find( item => item.prop == 'workspace_name')
              if (column && column.onchange) {
                column.onchange({...prop.self, mode: 'init_custom_form', form: dialogInfo.form, column, columns: dialogInfo.columns})
              }
            }
            dialogInfo.visible = true
          },
          hide: (param) => {
            const {dialogInfo} = param
            dialogInfo.visible = false
            dialogInfo.form.tip = ''
            dialogInfo.form.file = null
            //dialogInfo.form.file_list = null
            // if(dialogInfo.refs.uploadRef) {
            //   dialogInfo.refs.uploadRef.clearFiles()
            // }

            // if (dialogInfo.loading && dialogInfo.form.name) {
            //   const form = {name: dialogInfo.form.name}
            //   post('base_import', 'cancel', form)
            //   .then( data => {
            //     dialogInfo.loading = false
            //   })
            //   dialogInfo.loading = false
            // }

          },
        })

        importDialogInfo.value.columns = funcSortColumns(prop.dialogInfo.columns, importDialogInfo.value.columns)


        prop.self.init_remote_list(importDialogInfo.value.columns, {query: unref(importDialogInfo).form})
        prop.self[prop.dialogInfo.name || 'importDialogInfo'] = importDialogInfo

        if (prop.dialogInfo.onInit) {
          prop.dialogInfo.onInit({...self, dialogInfo:importDialogInfo.value, columns: importDialogInfo.value.columns, form: importDialogInfo.value.form})
        }

        const baseMounted = () => {

        }

        return {
          importDialogInfo,
          baseMounted,
        };
    },
};
</script>

<style scoped>
</style>
